package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.PatientInfo;
import com.dgut.doctorservice.entity.Patientcase;
import com.dgut.doctorservice.entity.Registration;
import com.dgut.doctorservice.service.PatientInfoService;
import com.dgut.doctorservice.service.PatientcaseService;
import com.dgut.doctorservice.service.RegistrationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-18
 */
@RestController
@RequestMapping("/doctorservice/patientcase")
@CrossOrigin
public class PatientcaseController {

    @Autowired
    PatientcaseService caseService;

    @Autowired
    PatientInfoService patientInfoService;

    @Autowired
    RegistrationService registrationService;




    @ApiOperation("医生进行病人的病例填写")
    @PostMapping("writeCase")
    public Result writeCase(@RequestBody Patientcase myCase,@RequestParam(required = false)String id){
        caseService.save(myCase);
        Registration byId = registrationService.getById(id);
        System.out.println(byId);
        byId.setFlags(1);
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("id",byId.getId());
        registrationService.update(byId,wrapper);
        registrationService.saveOrUpdate(byId);
        System.out.println(byId);
        return Result.succ();
    }

    @ApiOperation("根据病人的id获取病人所有病例")
    @PostMapping("getCaseById/{id}")
    public Result getCaseById(@RequestParam String patientId){
        QueryWrapper<PatientInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("id",patientId);
        List<PatientInfo> list1 = patientInfoService.list(wrapper);
        QueryWrapper<Patientcase> wrapper1=new QueryWrapper<>();
        wrapper1.eq("p_id",patientId);
        List<Patientcase> list = caseService.list(wrapper1);
        Map<String,Object> data=new HashMap<>();
        data.put("patient",list1);
        data.put("case",list);
        return Result.succ().data(data);
    }

}

